package com.dnr.arch.kernel.model;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import com.dnr.arch.kernel.KernelFactory;
import com.dnr.arch.kernel.api.model.MetaClassInterface;
import com.dnr.arch.kernel.api.model.Value;
import com.dnr.arch.kernel.api.rules.Rule;
import com.dnr.arch.kernel.api.security.KernelDomain;
import com.dnr.arch.kernel.services.persistence.exceptions.PersistenceException;

public abstract class AbstractValue implements Value {

	private Collection<Rule> rules;

	public MetaClassInterface getMetaClass() {
		return new MetaClass(this);
	}

	public KernelDomain getDomain() {
		return null;
	}

	public Collection<Rule> getRules() {
		if (rules == null) {
			rules = new ArrayList<Rule>();
		}
		return rules;
	}

	public void setRule(Rule rule) {
		if(rules == null){
			rules = new ArrayList<Rule>();
		}
		rules.add(rule);
	}

	public Object getFieldValue(String fieldName) {
		return null;
	}

	public void synchronize() throws PersistenceException {
		KernelFactory.getPersistenceDriver().synchronize(this);
	}

	public void synchronizeAndCommit() throws PersistenceException {
		KernelFactory.getPersistenceDriver().synchronize(this);
	}
	
	public Object getCode(){
		try {
			Method m = getClass().getMethod("getId", null);
			return m.invoke(this, null);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
